package com.zhaorenxueapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.zhaorenxueapp.Xutils.MyCallBack;
import com.zhaorenxueapp.Text.TitleBuilder;
import com.zhaorenxueapp.Xutils.XUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.tab01)
public class WeixinFragment extends Fragment {
    private View view;
    private View.OnClickListener leftCilckListener;
    private View.OnClickListener rightCilckListener;
//    private MyAdapter adapter;
//    private List<Person> list=new ArrayList<>();
    private List<String> listUrl=new ArrayList<>();
    private List<String> listName=new ArrayList<>();
    @ViewInject(R.id.btn_get)
    Button btn_get;
    @ViewInject(R.id.btn_post)
    Button btn_post;

    public WeixinFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = x.view().inject(this, inflater, container);
        initTitle();

        return view;
    }

    private void initTitle() {
        /**
         * 1.设置左边的图片按钮显示，以及事件
         * 2.设置中间TextView显示的文字
         * 3.设置右边的图片按钮显示，并设置事件
         */
        new TitleBuilder(view)
//                .setLeftImageRes(R.drawable.layout_top_back)
                .setLeftTextOrImageListener(leftCilckListener)
                .setMiddleTitleText("微信")
//                .setRightImageRes(R.drawable.layout_right_menu)
                .setRightTextOrImageListener(rightCilckListener);

    }

    // 等同于@Event(value={R.id.btn_get,R.id.btn_post},type=View.OnClickListener.class)
    @Event(value = {R.id.btn_get, R.id.btn_post})
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_get:
                getxml();
                break;
            case R.id.btn_post:
                Toast.makeText(getActivity(), "get请求", Toast.LENGTH_LONG).show();
                break;
        }
    }
    private void getxml() {
        String url="http://flash.weather.com.cn/wmaps/xml/china.xml";
        XUtil.Get(url, null, new MyCallBack<String>(){
            @Override
            public void onSuccess(String xmlString) {
                super.onSuccess(xmlString);
                try{
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    XmlPullParser xmlPullParser = factory.newPullParser();
                    xmlPullParser.setInput(new StringReader(xmlString));
                    int eventType = xmlPullParser.getEventType();
                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        switch (eventType) {
                            case XmlPullParser.START_TAG:
                                String nodeName = xmlPullParser.getName();
                                if ("city".equals(nodeName)) {
                                    String pName = xmlPullParser.getAttributeValue(0);
                                    Log.e("TAG", "city is " + pName);
                                }
                                break;
                        }
                        eventType = xmlPullParser.next();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

        });
    }

//    @OnClick({R.id.btn_get, R.id.btn_post})
//    public void testButtonClick(View view) { // 方法签名必须和接口中的要求一致
//        switch (view.getId()) {
//            case R.id.btn_get:
//                Toast.makeText(getActivity(), "get请求", Toast.LENGTH_LONG).show();
//                break;
//            case R.id.btn_post:
//                Toast.makeText(getActivity(), "post请求", Toast.LENGTH_LONG).show();
//                break;
//        }
//    }
}
